package com.web.base.wechat.service;

import com.blue.core.tree.TreeBuilder;
import com.blue.jdbc.JdbcDataSource;
import com.web.base.wechat.dao.WechatMenuDao;
import com.web.base.wechat.model.WechatMenu;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 微信菜单业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月27日
 */
@Service
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class WechatMenuService
{
	@Autowired
	private WechatMenuDao wechatMenuDao;
	
	public WechatMenuService()
	{
	}
	
	public WechatMenu save(WechatMenu wechatMenu)
	{
		if (wechatMenu.getId() == null)
		{
			wechatMenuDao.save(wechatMenu);
		}
		else
		{
			wechatMenuDao.update(wechatMenu);
		}
		WechatMenu a = wechatMenuDao.get(wechatMenu.getId());
		return a;
	}
	
	public WechatMenu get(Integer id)
	{
		WechatMenu wechatMenu = wechatMenuDao.get(id);
		return wechatMenu;
	}

	public WechatMenu getAll(Integer id)
	{
		WechatMenu wechatMenu = wechatMenuDao.getSelect(id);
		return wechatMenu;
	}
	
	public Map<String, List<WechatMenu>> listMenu(WechatMenu wechatMenu)
	{
		List<WechatMenu> list = wechatMenuDao.list(wechatMenu);
		return TreeBuilder.buildMenu(list);
	}

	public Map<String, List<WechatMenu>> listRootMenu()
	{
		List<WechatMenu> list = wechatMenuDao.listRoot();
		return TreeBuilder.buildMenu(list);
	}
	
	public void delete(WechatMenu wechatMenu)
	{
		wechatMenuDao.delete(wechatMenu);
	}
	
}
